var $;
layui.extend({
    select:'{/}/static/js/formSelects/select'
}).define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate','select'], function (exports) {
    $ = layui.$;
    var table = layui.table;
    var select = layui.select;
    var form = layui.form;
    var laypage = layui.laypage;
    var cols = [
        [
            {field: 'relateId', type: 'checkbox', fixed: 'left', width: 50, align: 'center'},
            {field: 'cityName', title: '城市名称', align: 'center'},
            {field: 'storeName', title: '店铺名称', align: 'center', templet: function (row) {
                    let value = "--";
                    if (row.storeName){
                        value = row.storeName;
                    }
                    return value;
                }},
            {field: 'storeAddress', title: '店铺地址', align: 'center'},
            {field: 'merName', title: '商家名称', align: 'center'},
            {field: 'webSiteName', title: '站点名称', align: 'center'},
            {field: 'itemConsumedNumber', title: '优惠券核销数量', align: 'center'},
            {field: 'status', title: '状态', align: 'center',templet:function(row){
                if(row.status==1){
                    return "冻结";
                }
                else if(row.status==2){
                    return "正常";
                }
            }},
            {field: 'createTime', title: '创建时间', align: 'center', templet: function (row) {
                var createTime = "";
                if (row.createTime){
                    createTime = zmnUtil.createTime(row.createTime);
                }
                return createTime;
            }},
            {field: 'updateTime', title: '状态更新时间', align: 'center', templet: function (row) {
                var updateTime = "--";
                if (row.updateTime){
                    updateTime = zmnUtil.createTime(row.updateTime);
                }
                return updateTime;
            }}

        ]
    ];
    let tableRender = function (data) {
        table.render({
            elem: "#zmn-review-table",
            toolbar: "#zmn-review-table-tool",
            title: "分类列表",
            where: data,
            data: json.items,
            limit: json.pageSize,
            cols: cols,
            page: true,
            height: 'full-120'
        });
    };
    tableRender();

    //分页加载数据，必须在表格初始化后
    laypage.render(zmnPage.laypageOptions(json));
    // form.on('submit(query-submit)', function (obj) {
    //     tableRender(obj.field);
    //     return false;
    // });
    //
    // var tableRender = function (data) {
    //     zmnLayUtil.layTableRender(table, cols, "zmn-review-table", data, "/coup/review/loadData.action",{limit:50,page:true});
    // };

    // /**
    //  * 描述: 初始化table
    //  **/
    // tableRender({merId : $("#merId").val()});

    /**
     * 描述: 删除
     **/
    $(document).on('click', '#del ', function () {
        var checkData = table.checkStatus('zmn-review-table').data;
        if (checkData.length < 1){
            layer.msg("请选择需要删除的配置");
            return false;
        }else {
            var relateIds = new Array();
            for (var i = 0;i<checkData.length;i++){
                relateIds.push(checkData[i].relateId);
            }
            layer.confirm('确定删除配置信息', {
                btn: ['确定'] //按钮
            }, function(){
                $.ajax({
                    type: "POST",
                    url: "/coup/review/delCoupStore.action",
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify({relateIds:relateIds}),
                    success: function (json) {
                        if (json.status == 200) {
                            tableRender({merId : $("#merId").val()});
                        }
                        layer.msg(json.message);
                    }
                });
            });
        }
    });
    /**
     * 描述: 添加
     **/
    $(document).on('click', '#add ', function () {
        zmnLayUtil.openIframeCustom(layer, "/coup/review/add.action", {title: "新增店铺", shade: 0.8,area : ['90%','60%']});
    });
    /**
     * 描述: 冻结店铺
     **/
    $(document).on('click','#freeze',function () {
        var checkData=table.checkStatus('zmn-review-table').data;
        if(checkData.length<1){
            layer.msg("请选择所要冻结的店铺");
            return false;
        }
        else {
            var relateIds = new Array();
            for (var i = 0; i <checkData.length ; i++) {
                relateIds.push(checkData[i].relateId);
            }
            layer.confirm("确定要冻结所选择的店铺?",{
                btn:['确定']
            },function () {
                $.ajax({
                    type: "POST",
                    url: "/coup/review/updateStatus.action",
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify({relateIds:relateIds,statusOperateType:1}),
                    success: function (json) {
                        if (json.status == 200) {
                            tableRender({merId : $("#merId").val()});
                        }
                        layer.msg(json.message);
                    }
                });
            })
        }
    });
    /**
     * 描述: 解冻店铺
     **/
    $(document).on('click','#unfreeze',function () {
        var checkData=table.checkStatus('zmn-review-table').data;
        if(checkData.length<1){
            layer.msg("请选择所要冻结的店铺");
            return false;
        }
        else {
            var relateIds = new Array();
            for (var i = 0; i <checkData.length ; i++) {
                relateIds.push(checkData[i].relateId);
            }
            layer.confirm("确定要解冻所选择的店铺?",{
                btn:['确定']
            },function () {
                $.ajax({
                    type: "POST",
                    url: "/coup/review/updateStatus.action",
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify({relateIds:relateIds,statusOperateType:2}),
                    success: function (json) {
                        if (json.status == 200) {
                            tableRender({merId : $("#merId").val()});
                        }
                        layer.msg(json.message);
                    }
                });
            })
        }
    });
    // 公司商家下拉联动
    var selectOption = {
        elem: '#companyCode',
        child: {
            url: '/coup/review/getCoupStoresByCompanyCode.action',
            param: 'companyCode',
            elem: '#merId',
            // child：{}
        }
    };
    select.render(selectOption);
    //对外暴露的接口
    exports('item_list', {});
});
